/***************************************************************************
 *   Copyright (C) 2007 by Anders Nore									   								 *
 *   andenore@start.no   												   												 *
 *                                                                         *
 *   Permission is hereby granted, free of charge, to any person obtaining *
 *   a copy of this software and associated documentation files (the       *
 *   "Software"), to deal in the Software without restriction, including   *
 *   without limitation the rights to use, copy, modify, merge, publish,   *
 *   distribute, sublicense, and/or sell copies of the Software, and to    *
 *   permit persons to whom the Software is furnished to do so, subject to *
 *   the following conditions:                                             *
 *                                                                         *
 *   The above copyright notice and this permission notice shall be        *
 *   included in all copies or substantial portions of the Software.       *
 *                                                                         *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       *
 *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    *
 *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*
 *   IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR     *
 *   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
 *   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
 *   OTHER DEALINGS IN THE SOFTWARE.                                       *
 ***************************************************************************/

#include "duxstring.h"
#include "mcastchat.h"
#include <stdio.h>

int cutString(char **inputstring, char *seperator, char **out)
{
	static char **ap;
	char *text;
	static char *argv[10];
	static char **output;
	
	output = Malloc(MAX_ARGS);
	
	int index = 0;
	while( (text = strsep(inputstring, seperator)) != NULL) {
		output[index] = Malloc(sizeof(text));

// 		DEBUG_PRINT_STR("Text buffer:", text);
		strncpy(output[index], text, strlen(text) + 1);	/* make sure we null-terminate */
		if( ++index > MAX_ARGS )
			break;

	}

  *out = output;
	
	return index;
}

void freeCutString(char **buf)
{
	int i;
	for( i=0; i<MAX_ARGS; i++) {
		free(buf[i]);
	}
	free(buf);
	
	return;
}

int extractStringAtPos(int pos, char *seperator, char **inputstring, char **out)
{
	int i;
	char **buffer;
	int buflen = cutString(inputstring, seperator, &buffer);
	
	char *ret = Malloc(sizeof(buffer[pos]) + 1);
	strncpy(ret, buffer[pos], strlen(buffer[pos])+1);
	*out = ret;	/* save to out */

	int len = sizeof(*inputstring);
	char *ret2 = Malloc(strlen(&inputstring) + 1);
	
// 	DEBUG_PRINT_INT("inputstring length:", strlen(&inputstring) + 1);
// 	DEBUG_PRINT_INT("buffer length:", buflen);
	
	for( i=0; i < buflen ; i++) {
		if(i == pos)
			continue;
		
// 		DEBUG_PRINT_STR("Buffer", buffer[i]);
		strcat(ret2, buffer[i]);
		strcat(ret2, " ");
	}
	
	*inputstring = ret2;	/* save our new inputstring */
	
// 	DEBUG_PRINT_STR("ret :", ret);
// 	DEBUG_PRINT_STR("ret2:", ret2);
	freeCutString( buffer );
	
	return 0;
}


